package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.EventTag;


/**
 * 投票弹窗
 */
public class VotePop extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private LinearLayout llAgree;
    private LinearLayout llOppose;
    private TextView tvArgee;
    private TextView tvOppose;

    private int agreeCount;
    private int opposeCount;

    public VotePop(Context context, int agreeCount, int opposeCount) {
        this.mContext = context;
        this.agreeCount = agreeCount;
        this.opposeCount = opposeCount;
        setWidth(RT.getScreenWidth() * 10 / 13);
        setHeight(-2);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        setAnimationStyle(R.style.ReportPopAnim);
        View view = LayoutInflater.from(context).inflate(R.layout.popup_vote, null);
        setContentView(view);
        initView(view);
    }

    private void initView(View view) {
        llAgree = (LinearLayout) view.findViewById(R.id.llAgree);
        llOppose = (LinearLayout) view.findViewById(R.id.llOppose);
        tvArgee = (TextView) view.findViewById(R.id.tvArgee);
        tvOppose = (TextView) view.findViewById(R.id.tvOppose);
        tvArgee.setText(mContext.getString(R.string.agreeWith, String.valueOf(agreeCount)));
        tvOppose.setText(mContext.getString(R.string.oppose, String.valueOf(opposeCount)));
        llAgree.setOnClickListener(this);
        llOppose.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llAgree:
                EventManager.ins().sendEvent(EventTag.DO_AGREE, 0, 0, null);
                dismiss();
                break;
            case R.id.llOppose:
                EventManager.ins().sendEvent(EventTag.DO_OPPOSITION, 0, 0, null);
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
